package com.luhuiguo.cms.client.presenter;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gen2.table.client.MutableTableModel;
import com.google.gwt.gen2.table.client.TableModelHelper;
import com.google.gwt.gen2.table.client.TableModelHelper.Request;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.luhuiguo.cms.shared.event.ArticlesLoadedEvent;
import com.luhuiguo.cms.shared.event.LoadArticlesEvent;
import com.luhuiguo.cms.shared.event.LoadArticlesEventHandler;
import com.luhuiguo.cms.shared.event.LogoutEvent;
import com.luhuiguo.cms.shared.event.LogoutEventHandler;
import com.luhuiguo.cms.shared.model.ArticleModel;
import com.luhuiguo.cms.shared.model.ChannelModel;
import com.luhuiguo.cms.shared.rpc.FetchArticles;
import com.luhuiguo.cms.shared.rpc.FetchArticlesResult;
import com.luhuiguo.gwt.dispatcher.client.DispatcherAsync;
import com.luhuiguo.gwt.presenter.client.EventBus;

public class ArticleTableModel extends MutableTableModel<ArticleModel> {

    private EventBus eventBus;
    private DispatcherAsync dispatcher;

    private Long channel;

    @Inject
    public ArticleTableModel(EventBus eventBus, DispatcherAsync dispatcher) {
        this.eventBus = eventBus;
        this.dispatcher = dispatcher;

        eventBus.addHandler(LoadArticlesEvent.TYPE, new LoadArticlesEventHandler() {

            public void onLoadArticles(LoadArticlesEvent event) {
                channel = event.getChannel();
            }
        });

        eventBus.addHandler(LogoutEvent.TYPE, new LogoutEventHandler() {

            public void onLogout(LogoutEvent logoutEvent) {
                channel = null;

            }
        });

    }

    @Override
    protected boolean onRowInserted(int beforeRow) {
        return true;
    }

    @Override
    protected boolean onRowRemoved(int row) {
        return true;
    }

    @Override
    protected boolean onSetRowValue(int row, ArticleModel rowValue) {
        return true;
    }

    @Override
    public void requestRows(final Request request, final Callback<ArticleModel> callback) {

        if (channel == null) {
            callback.onRowsReady(request, new TableModelHelper.Response<ArticleModel>() {

                @Override
                public Iterator<ArticleModel> getRowValues() {
                    return new ArrayList<ArticleModel>().iterator();
                }

            });
            
            return;
        }

        dispatcher.execute(new FetchArticles(channel, request.getStartRow(), request.getNumRows()),
                new AsyncCallback<FetchArticlesResult>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        GWT.log("FetchArticles", caught);
                        callback.onFailure(caught);
                    }

                    @Override
                    public void onSuccess(final FetchArticlesResult result) {

                        eventBus.fireEvent(new ArticlesLoadedEvent(channel, result.getArticles()));
                        TableModelHelper.Response<ArticleModel> response = new TableModelHelper.Response<ArticleModel>() {
                            @Override
                            public Iterator<ArticleModel> getRowValues() {
                                if (result != null && result.getArticles() != null) {
                                    return result.getArticles().iterator();
                                } else {
                                    return new ArrayList<ArticleModel>().iterator();
                                }
                            }
                        };
                        setRowCount(result.getTotalCount());
                        callback.onRowsReady(request, response);

                    }

                });

    }
}
